package com.caigou.service.impl;

import com.caigou.bean.cms.param.ChannelSpecialIssueParam;
import com.caigou.component.HtmlBuildSpecialIssueComponent;
import com.caigou.enums.ResponseEnum;
import com.caigou.mapper.cms.ChannelSpecialIssueMapper;
import com.caigou.pojo.ChannelSpecialIssue;
import com.caigou.pojo.ChannelSpecialIssueTitle;
import com.caigou.service.ChannelSpecialIssueService;
import com.caigou.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service("channelSpecialIssueService")
public class ChannelSpecialIssueServiceImpl implements ChannelSpecialIssueService {

    @Resource
    private ChannelSpecialIssueMapper channelSpecialIssueMapper;

    @Resource
    private HtmlBuildSpecialIssueComponent htmlBuildSpecialIssueComponent;

    @Override
    public Result loadChannelSpecialIssueTitle(ChannelSpecialIssueParam param) {
        ChannelSpecialIssueTitle channelSpecialIssueTitle = channelSpecialIssueMapper.loadChannelSpecialIssueTitle(param);
        return Result.success(channelSpecialIssueTitle);
    }

    @Override
    public Result loadChannelSpecialIssueList(ChannelSpecialIssueParam param) {
        List<ChannelSpecialIssue> list = channelSpecialIssueMapper.loadChannelSpecialIssueList(param);
        return Result.success(list);
    }

    @Override
    public Result updateChannelSpecialIssueStatus(ChannelSpecialIssueParam param) {
        // 调整特刊发布状态
        channelSpecialIssueMapper.updateChannelSpecialIssueStatus(param);
        return Result.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result releaseChannelSpecialIssue(ChannelSpecialIssueParam param) {
        int status = channelSpecialIssueMapper.loadChannelSpecialIssueStatus(param.getPlateId());
        if (status == 1){
            // 发布状态需要验证必须有一条标题、一条内容
            int issueTitleCount = channelSpecialIssueMapper.loadChannelSpecialIssueTitleCount(param.getPlateId());
            int issueCount = channelSpecialIssueMapper.loadChannelSpecialIssueCount(param.getPlateId());
            if (issueTitleCount == 0 || issueCount == 0) return Result.error(ResponseEnum.SPECIAL_ISSUE_RELEASE_ERROR);
        }
        // 更新特刊静态资源
        htmlBuildSpecialIssueComponent.buildSpecialIssueHtml(param.getPlateId());
        return Result.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result saveChannelSpecialIssueTitle(ChannelSpecialIssueParam param) {
        if (null == param.getPlateId() || param.getTitle().equals("")) return Result.error(ResponseEnum.SPECIAL_ISSUE_PARAM_ERROR);
        int issueTitleCount = channelSpecialIssueMapper.loadChannelSpecialIssueTitleCount(param.getPlateId());
        if (issueTitleCount == 1) return Result.error(ResponseEnum.SPECIAL_ISSUE_TITLE_COUNT_ERROR);
        ChannelSpecialIssueTitle channelSpecialIssueTitle = new ChannelSpecialIssueTitle();
        channelSpecialIssueTitle.setPlateId(param.getPlateId());
        channelSpecialIssueTitle.setTitle(param.getTitle());
        channelSpecialIssueTitle.setUrl(param.getUrl());
        channelSpecialIssueMapper.saveChannelSpecialIssueTitle(channelSpecialIssueTitle);
        return Result.success(channelSpecialIssueTitle);
    }

    @Override
    public Result deleteChanelSpecialIssueTitle(ChannelSpecialIssueParam param) {
        channelSpecialIssueMapper.deleteChanelSpecialIssueTitle(param.getId());
        return Result.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result updateChanelSpecialIssueTitle(ChannelSpecialIssueParam param) {
        if (null == param.getId() || param.getTitle().equals("")) return Result.error(ResponseEnum.SPECIAL_ISSUE_PARAM_ERROR);
        ChannelSpecialIssueTitle channelSpecialIssueTitle = new ChannelSpecialIssueTitle();
        channelSpecialIssueTitle.setId(param.getId());
        channelSpecialIssueTitle.setTitle(param.getTitle());
        channelSpecialIssueTitle.setUrl(param.getUrl());
        channelSpecialIssueMapper.updateChanelSpecialIssueTitle(channelSpecialIssueTitle);
        return Result.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result saveChannelSpecialIssue(ChannelSpecialIssueParam param) {
        if (null == param.getPlateId() || param.getTitle().equals("") || param.getUrl().equals("")) return Result.error(ResponseEnum.SPECIAL_ISSUE_PARAM_ERROR);
        // 获取当前频道最大排序号
        int maxIndex = channelSpecialIssueMapper.loadChannelSpecialIssueMaxIndex(param.getPlateId());
        ChannelSpecialIssue channelSpecialIssue = new ChannelSpecialIssue();
        channelSpecialIssue.setPlateId(param.getPlateId());
        channelSpecialIssue.setTitle(param.getTitle());
        channelSpecialIssue.setUrl(param.getUrl());
        channelSpecialIssue.setShowIndex(maxIndex + 1);
        channelSpecialIssueMapper.saveChannelSpecialIssue(channelSpecialIssue);
        return Result.success(channelSpecialIssue);
    }

    @Override
    public Result deleteChannelSpecialIssue(ChannelSpecialIssueParam param) {
        channelSpecialIssueMapper.deleteChannelSpecialIssue(param.getId());
        return Result.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result updateChannelSpecialIssue(ChannelSpecialIssueParam param) {
        if (null == param.getId() || param.getTitle().equals("") || param.getUrl().equals("")) return Result.error(ResponseEnum.SPECIAL_ISSUE_PARAM_ERROR);
        ChannelSpecialIssue channelSpecialIssue = new ChannelSpecialIssue();
        channelSpecialIssue.setId(param.getId());
        channelSpecialIssue.setTitle(param.getTitle());
        channelSpecialIssue.setUrl(param.getUrl());
        channelSpecialIssue.setShowIndex(param.getShowIndex());
        channelSpecialIssueMapper.updateChannelSpecialIssue(channelSpecialIssue);
        return Result.success(channelSpecialIssue);
    }

    @Override
    public Result updateChannelSpecialIssueShowIndex(List<ChannelSpecialIssueParam> list) {
        list.forEach(item -> channelSpecialIssueMapper.updateChannelSpecialIssueShowIndex(item.getId(),item.getShowIndex()));
        return Result.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result cleanChannelSpecialIssue(ChannelSpecialIssueParam param) {
        // 清除特刊标题数据
        channelSpecialIssueMapper.cleanChannelSpecialIssueTitle(param.getPlateId());
        // 清除特刊列表数据
        channelSpecialIssueMapper.cleanChannelSpecialIssue(param.getPlateId());
        return Result.success(ResponseEnum.SUCCESS);
    }
}
